<template>
  <div id="box">
    <!-- <el-radio-group v-model="chelaxy" style="margin-bottom: 10px;">
      <el-radio-button label="卒中" />
    </el-radio-group> -->
    <el-row>
      <el-col :span="22" style="margin-bottom: 10px;">
        <span style="font-size: 14px;">绿道时间：</span>
        <el-select size="small" v-model="chelaxy" placeholder="请选择">
          <el-option label="卒中" value="卒中">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2" style="margin-bottom: 10px;">
        <el-button type="primary" @click="saveIsChecked()">保存状态</el-button>
      </el-col>
    </el-row>
    <el-table ref="multipleTable" border height="640" :data="tableData" style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55">
      </el-table-column>
      <el-table-column align="center" prop="green_type" label="类型" width="200">
        <template slot-scope="scope">
          <span v-if="scope.row.green_type =='chestpain'">胸痛</span>
          <span v-else>卒中</span>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="名称" align="center">
      </el-table-column>
      <el-table-column align="center" label="状态" width="200">
        <template slot-scope="scope">
          <span v-if="scope.row.isChecked=='1'">使用中</span>
          <span v-else="scope.row.isChecked=='0'">禁用</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="created_time" label="创建时间">
      </el-table-column> -->

    </el-table>
  </div>
</template>

<script>
  import {
    selectShowTimes,
    saveShowTimeByHospitalId
  } from '@/api/back-management/greenway-set/greenTime'
  export default {
    name: 'GreenTime',
    data() {
      return {
        chelaxy: '卒中',
        tableData: [],
        usedList: [],
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        let data = {
          hospitalId: this.$store.getters.hospitalId
        }
        selectShowTimes(data).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.list
            this.usedList = res.data.list.filter(function(item, index) {
              if (item.isChecked == '1') {
                return item.id
              }
            })
            this.usedList.forEach(row => {
              this.$nextTick(() => {
                this.$refs.multipleTable.toggleRowSelection(row, true)
              })
            })
          } else {
            this.$message.warning('获取列表失败！')
          }
        }).catch(err => {
          this.$message.error(err.message)
        })
      },
      handleSelectionChange(val) {
        this.usedList = val
      },
      saveIsChecked() {
        let newArr = []
        this.usedList.forEach(item => {
          newArr.push(item.id)
        })
        let showTimeList = Array.from(new Set(newArr))
        let data = {
          "hospitalId": Number(this.$store.getters.hospitalId),
          "showTimeList": showTimeList
        }
        saveShowTimeByHospitalId(data).then(res => {
          if (res.code == 0) {
            this.$message.success('保存成功！')
            this.getList()
          } else {
            this.$message.error('保存失败！')
          }
        }).catch(err => {
          this.$message.error(err.message)
        })
      },
    }
  }
</script>

<style scoped>
</style>
